package core

import (
	"github.com/spf13/viper"
	"strings"
	"sync"
	"tone-agent/entity"
)

func SendHeartbeat() {
	// 写入时间戳到本机，供心跳自检使用
	WriteHeartbeat()
	if viper.Get("mode") == "active" {
		SyncHeartbeat()
	}
}

func PullTasks() {
	var mutex = sync.Mutex{}
	mutex.Lock()
	defer mutex.Unlock()
	tasks := PullTask()
	if len(tasks) != 0 {
		MyLogger.WithFields(LogFields{"tasks": len(tasks)}).Info("Pull tasks scheduler")
	}
	go ProcessTask(tasks)
}

func SyncResults() {
	files := ScanResult()
	for _, f := range files {
		tid := strings.Trim(f, ".json")
		result, _ := ReadResult(tid)
		if result["status"] == entity.TaskCompletedStatus {
			SyncResult(result)
		}
	}

}
